<?php

$Invoice  = (object)$data['Invoice'];
$Services = $data['InvoiceService'];
$Campaign = (object)$data['Campaign'];
$Tax      = (object)$Campaign->Tax;
$Client   = (object)$Campaign->Client;
$ExRate   = (object)$data['ExchangeRate'];
$Settings = (object)$settings;

?>

<?= $this->Html->css('print.css', null, array('media'=>'print', 'inline'=>false)) ?>
<?= $this->Html->css('pdf.css', null, array('inline'=>false)) ?>

<?= $this->Form->button('Štampa', array('onclick' => 'window.print()')) ?>

<div id="document">
<div class="left" style="width:50%">
    <p><?= $Settings->name ?></p>
    <p><?= $Settings->address ?></p>
    <p><?= $Settings->zipcode ?> <?= $Settings->city ?></p>
    <p>PIB: <?= $Settings->pib ?></p>
    <p>
		TR:
		<? 
			for ($i=1; $i<=4; $i++) {
				$account = "account{$i}";
				if (! empty($Settings->$account)) {
					echo $Settings->$account . "<br>";
				}
			}
		?> 
	</p>
</div>
<div class="right" style="width:40%">
	<p><?= $Client->name ?></p>
    <p><?= $Client->address ?></p>
    <p><?= $Client->zipcode ?> <?= $Client->city ?></p>
    <p>PIB: <?= $Client->pib ?></p>
</div>

<br clear="all">
<div class="left" style="width:50%">
    <p class="first"><strong class="label">Mesto i datum izdavanja</strong>: Beograd, <?= date_local($Invoice->date) ?></p>
    <p><strong class="label">Način plaćanja</strong>: Virman</p>
    <p><strong class="label">Datum prometa usluga</strong>: <?= date_local($Invoice->date) ?></p>
	<p><strong class="label">Valuta plaćanja</strong>: <?= date_local($Invoice->date_valuta) ?></p>
</div>
<div class="right" style="width:40%">
    <p class="first"><strong class="label" style="width:85px">Srednji kurs</strong>: <?= currency_format($ExRate->rate, 4) ?></p>
    <p><strong class="label" style="width:85px">Neto Cena</strong>: <span id="total-neto"></span> <?= CURRENCY_SIGN ?></p>
</div>

<br clear="all">
<h1><?= $Invoice->title ?> broj: <?= $Invoice->number ?></h1>

<?php
	$productionMountPrice = 0;
	$productionMountLabels = array();
	$total = 0; 
?>
<table>
    <tr>
        <th align="left" width="1%">R.B.</th>
        <th align="left">Usluga</th>
        <th align="right" width="10%">Iznos</th>
    </tr>
    <? foreach ($Campaign->Location as $k=>$loc): ?>
    <?php
        $CampaignLocation = (object)$Campaign->CampaignsLocation[$k];
		$rentPrice 	      = $CampaignLocation->price->rent * $ExRate->rate;
		
		$startMonth        = date("m", strtotime($CampaignLocation->start));
		$invoiceStartMonth = date("m", strtotime($Invoice->date_valuta));
		
		if ($startMonth == $invoiceStartMonth) {
			$productionMountPrice += (
									$CampaignLocation->price->production +
									$CampaignLocation->price->mount
							   ) * $ExRate->rate;
		   $productionMountLabels[] = $CampaignLocation->production;
		}
		
		$total += $rentPrice + $productionMountPrice;
    ?>
    <tr>
        <td><?= $k+1 ?>.</td>
        <td><?= $Services[$k]['service'] ?></td>
        <td align="right"><?= currency_format($rentPrice) ?></td>
    </tr>
    <? endforeach ?>
    
    <? $productionMountLabels = array_unique($productionMountLabels) ?>
    <? if ($productionMountPrice || (in_array(PRODUCTION_GRATIS, $productionMountLabels))): ?>
    <tr>
        <td><?= $k+2 ?>.</td>
        <td>
        
        <? if (in_array(PRODUCTION_GRATIS, $productionMountLabels) || in_array(PRODUCTION_ALL, $productionMountLabels)) {
        	echo 'Produkcija i montaža';
		} else if (in_array(PRODUCTION_MOUNT, $productionMountLabels)) {
			echo 'Montaža';
		} ?></td>
        <td align="right">
        	<? if ($productionMountPrice>0): ?>
        		<?= currency_format($productionMountPrice) ?>
        	<? else: ?>
        		GRATIS
        	<? endif ?>
    	</td>
    </tr>
    <? endif ?>
        
    <?php
        $taxName  = $Tax->name;
        $taxValue = (float)$Tax->value;
        $tax      = $total * $taxValue / 100;
    ?>
    <tr class="total separator">
        <td colspan="2" align="right">Osnovica za <?= $taxName ?>:</td>
        <td align="right"><?= currency_format($total) ?></td>
    </tr>
    <tr class="total">
        <td colspan="2" align="right"><?= $taxName ?> (<?= $taxValue?>%):</td>
        <td align="right"><?= currency_format($tax) ?></td>
    </tr>
    <tr class="total">
        <td class="total" colspan="2" align="right">UKUPNO:</td>
        <td class="total" align="right"><?= currency_format($total + $tax) ?></td>
    </tr>
</table>
<script type="text/javascript">
	$('#total-neto').html('<?= currency_format($total / $ExRate->rate) ?>');
</script>


<br clear="all">
<div id="signature">
    <?= $Settings->shortname ?>,<br><?= $Settings->director ?>
</div>

</div>
